
package bibliotheque.server;

import bibliotheque.transaction.TransactionId;
import bibliotheque.document.DocumentId;
import bibliotheque.wave.WaveAction;
import bibliotheque.wave.WaveAgent;
import java.util.Enumeration;

public class ReadAction extends WaveAction {
    private TransactionId tid;
    private DocumentId did;
    private String document;
    
    
    public ReadAction(TransactionId tid,DocumentId did) {
        this.tid = tid;
        this.did = did;
        this.document = null;
    }
    
    public void PropagateBefore(WaveAgent agent) throws Exception {
        if (agent instanceof Server) {
            Server server = (Server)agent;
            
            try {
                server.mutex.acquireUninterruptibly();
                if(server.state == Server.State.SUBSCRIBING) throw new Exception("Selection failed cause server state is subscribing.");
                if(server.state == Server.State.STARTING) throw new Exception("Selection failed cause server state is starting.");
                if(server.state == Server.State.STOPPING) throw new Exception("Selection failed cause server state is stopping.");
                if(server.state == Server.State.STOP) throw new Exception("Selection failed cause server state is stopped.");
                
                this.document = server.transaction_manager.ReadInit(this.wave_id, tid, did);
                
                
            } catch (Exception ex) {
                server.ErrorOccured(ex.getMessage());
                throw ex;
            } finally {
                server.mutex.release();
            }
        }
    }
    
    public void Acknowledge(WaveAgent agent) throws Exception {
        Server server ;
        if (agent instanceof Server) {
            server = (Server)agent;
            try {
                server.mutex.acquireUninterruptibly();
                if(server.state == Server.State.SUBSCRIBING) throw new Exception("Free failed cause server state is subscribing.");
                if(server.state == Server.State.STARTING) throw new Exception("Free failed cause server state is starting.");
                if(server.state == Server.State.STOPPING) throw new Exception("Free failed cause server state is stopping.");
                if(server.state == Server.State.STOP) throw new Exception("Free failed cause server state is stopped.");
                
                server.transaction_manager.ReadUpdate(this.wave_id, this.document);
                
            } catch (Exception ex) {
                server.ErrorOccured(ex.getMessage());
                throw ex;
            } finally {
                server.mutex.release();
            }
        }
    }
    
    public void LocalEnd(WaveAgent agent) throws Exception {
        Server server ;
        if (agent instanceof Server) {
            server = (Server)agent;
            try {
                server.mutex.acquireUninterruptibly();
                if(server.state == Server.State.SUBSCRIBING) throw new Exception("Free failed cause server state is subscribing.");
                if(server.state == Server.State.STARTING) throw new Exception("Free failed cause server state is starting.");
                if(server.state == Server.State.STOPPING) throw new Exception("Free failed cause server state is stopping.");
                if(server.state == Server.State.STOP) throw new Exception("Free failed cause server state is stopped.");
                
                this.document = server.transaction_manager.ReadResult(this.wave_id);
                
            } catch (Exception ex) {
                server.ErrorOccured(ex.getMessage());
                throw ex;
            } finally {
                server.mutex.release();
            }
        }
    }
    
    public void GlobalEnd(WaveAgent agent) throws Exception {
        if (agent instanceof Server) {
            Server server = (Server)agent;
            try {
                server.mutex.acquireUninterruptibly();
                if(server.state == Server.State.SUBSCRIBING) throw new Exception("Selection failed cause server state is subscribing.");
                if(server.state == Server.State.STARTING) throw new Exception("Selection failed cause server state is starting.");
                if(server.state == Server.State.STOPPING) throw new Exception("Selection failed cause server state is stopping.");
                if(server.state == Server.State.STOP) throw new Exception("Selection failed cause server state is stopped.");
                
                if(this.document != null) server.clients.get(this.tid.GetClient()).SetReadResult(this.did, this.document);
                else {
                    InvalidateAction action = new InvalidateAction(this.tid);
                    server.Initiate(action);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                server.ErrorOccured("\nRead error " + ex.getMessage() + "\n");
                throw ex;
            }finally{
                server.mutex.release();
            }
            
        }
    }
    
}
